#include <stdlib.h>
#include <string.h>
#include "stack.h"
#include <assert.h>
void stack_create(Stack *pstack)
{
	
	pstack->increment = 10;
	pstack->initial_size = 50;
	pstack->countelements = 0;
	pstack->nowsize = pstack->initial_size;
	pstack->ykazatelnadno = (Pointer *)malloc(50*sizeof(Pointer));
}

void stack_destroy(Stack *pstack)
{
	pstack->countelements = 0;
	pstack->nowsize = 0;
	free(pstack->ykazatelnadno);
}

void stack_push(Stack *pstack, Pointer value)
{
	if (pstack->countelements == pstack->nowsize)
	{
		Pointer * buffnew = (Pointer*)malloc((pstack->nowsize + pstack->increment) * sizeof(int));
		memcpy(buffnew, pstack->ykazatelnadno, pstack->nowsize * sizeof(int));
		free(pstack->ykazatelnadno);
		pstack->ykazatelnadno = buffnew;
		pstack->nowsize += pstack->increment;
	}
	pstack->ykazatelnadno [pstack->countelements++] = value;
}

size_t stack_size(Stack *pstack)
{
	return(pstack->countelements);
}

Pointer stack_pop(Stack *pstack)
{
	if(pstack->countelements == 0)
	{
		assert(0);
		return(0);
	}
	return(pstack->ykazatelnadno[--(pstack->countelements)]);
}

Pointer stack_peek(Stack *pstack)
{
	if(pstack->countelements == 0)
	{
		assert(0);
		return(0);
	}
	return(pstack->ykazatelnadno[pstack->countelements-1]);
}

void stack_tune(Stack *pstack, size_t initial_size, size_t increment)
{
	pstack->initial_size = initial_size;
	pstack->increment = increment;
}